package com.codeyang.mocktest2.controller;

import com.codeyang.mocktest2.entity.TUser;
import com.codeyang.mocktest2.service.TUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 描述: describe
 *
 * @author shengyang5
 * @version 2021/7/20 16:44
 */
@Api(value = "测试", tags = "测试专属")
@RequestMapping("api")
@RestController
public class MvcController {

    @Autowired
    private TUserService userService;

    @ApiOperation(value = "新增", notes = "新增数据")
    @PostMapping("save")
    public Object method02(TUser tUser) {
        TUser newUser = userService.saveUser(tUser);
        return newUser;
    }

    @ApiOperation(value = "删除数据", notes = "根据名称删除")
    @DeleteMapping("del")
    public Object delByName(String name) {
        int row = userService.delByName(name);
        return row > 0 ? "success" : "error";
    }

    @ApiOperation(value = "修改数据")
    @PutMapping("upda")
    public Object method01(TUser tUser) {

        int row = userService.updateUser(tUser);

        return row > 0 ? "success" : "error";
    }

    @ApiOperation(value = "获取id为1的对象")
    @GetMapping("get")
    public Object get() {
        TUser tUser = userService.selectByPrimaryKey(1);

        return tUser;
    }

    @ApiOperation(value = "获取指定id的对象")
    @GetMapping(value = "get/{id}")
    public Object index(@PathVariable("id") Integer idss) {
        TUser tUser = userService.selectByPrimaryKey(idss);
        return tUser;
    }

    @ApiOperation(value = "获取指定id的对象/{id}")
    @GetMapping("getId")
    public Object get2(@RequestParam("id") Integer id) {
        TUser tUser = userService.selectByPrimaryKey(id);

        return tUser;
    }

    @GetMapping("getByid")
    public Object getByid(Integer id) {
        TUser tUser = userService.selectByPrimaryKey(id);

        return tUser;
    }

    @ApiOperation(value = "获取add与name一致的数据集合")
    @PostMapping("getNameAndAdd")
    public Object get2(@RequestParam("address") String address, @RequestParam("name") String name) {
        List<TUser> tUser = userService.queryAddressAndName(address, name);
        return tUser;
    }

    @ApiOperation(value = "获取全部数据集合")
    @GetMapping("all")
    public Object getAll() {
        List<TUser> list = userService.queryAll();
        return list;
    }

    @ApiOperation(value = "使用json修改数据")
    @PutMapping("putObj")
    public Object putObj(@RequestBody TUser tUser) {
        int i = userService.updateUser(tUser);
        return i > 0 ? "success" : "fail";
    }

    @ApiOperation(value = "上传文件")
    @PostMapping("upload")
    public Object upload(@RequestParam("uploadFile") MultipartFile file) {
        String content = null;
        try {
            content = userService.accpetFile(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content;
    }


}
